package com.yuji.aacar.controller.forgetpassword;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.bean.response.SendShortMessageResponse;
import com.yuji.aacar.bean.response.ValidatorShortMessageResponse;
import com.yuji.aacar.core.cores.event.CoreEventBusMessage;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.tools.Tools;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.countdownbutton.WCountDownButton;
import com.yuji.aacar.widgets.edittext.CheckEditText;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.yuji.aacar.service.BaseServiceMediator.VALIDATOR_SHORT_MESSAGE;

public class ForgetPasswordActivity extends BaseActivity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.forget_phone_number_et)
    CheckEditText forgetPhoneNumberEt;
    @BindView(R.id.forget_clear_iv)
    ImageView forgetClearIv;
    @BindView(R.id.forget_verify_code_et)
    CheckEditText forgetVerifyCodeEt;
    @BindView(R.id.forget_countdowntv)
    WCountDownButton forgetCountdowntv;
    @BindView(R.id.forget_next_btn)
    Button forgetNextBtn;
    private ForgetPasswordActivityVM forgetPasswordActivityVM;

    private String phone;
    private String verifyCode;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_forget_password_first);
        ButterKnife.bind(this);
        initTitle(true,"找回密码",null);
        super.onCreate(savedInstanceState);

    }

    @Override
    public void alreadyBindBaseViewModel() {
        super.alreadyBindBaseViewModel();
        //        requestParams =
    }

    @Override
    public void handleMessage(CoreEventBusMessage message) {
        super.handleMessage(message);
    }

    @OnClick({R.id.forget_clear_iv, R.id.forget_countdowntv, R.id.forget_next_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.forget_clear_iv:
                forgetPhoneNumberEt.setText("");
                break;
            case R.id.forget_countdowntv:
                if (isPhoneLegal()){
                    forgetCountdowntv.startCountDown();
                    requestParams = BaseServiceParams.sendShortMessage(phone,"1");
                    doTask(BaseServiceMediator.SEND_SHORT_MESSAGE);
                }
                break;
            case R.id.forget_next_btn:
                if (isLegal()){
                    requestParams = BaseServiceParams.validatorshortmessage(phone, verifyCode, "1");
                    doTask(VALIDATOR_SHORT_MESSAGE);
                }
                break;
        }
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.SEND_CHECK_CODE) {
            SendShortMessageResponse sendShortMessageResponse = (SendShortMessageResponse)response.getResponse();
            showToast(sendShortMessageResponse.getMessage());
        } else if(response.getRequestCode() == BaseServiceMediator.CHECK_CHECK_CODE){
            ValidatorShortMessageResponse sendShortMessageResponse = (ValidatorShortMessageResponse)response.getResponse();
            if (sendShortMessageResponse.isSuccess()){
                String nextStepCode = sendShortMessageResponse.getMessage();
                Intent intent = getIntent();
                intent.putExtra("phone", phone);
                intent.putExtra("code", nextStepCode);
                presentController(ResetPasswordActivity.class, intent);
            }
        }
    }

    private boolean isPhoneLegal() {
        phone = forgetPhoneNumberEt.getText().toString();
        if (!Tools.isPhoneNumberValid(phone)){
            showToast("请输入正确的手机号！");
            return false;
        }
        return true;
    }

    private boolean isLegal() {
        phone = forgetPhoneNumberEt.getText().toString();
        verifyCode = forgetVerifyCodeEt.getText().toString();
        if (!Tools.isPhoneNumberValid(phone)){
            showToast("请输入正确的手机号！");
            return false;
        }
        if (TextUtils.isEmpty(verifyCode)){
            showToast("请输入短信验证码！");
            return false;
        }
        return true;
    }
}
